package com.jf.fileupload.webutils;

import org.apache.commons.fileupload.FileItem;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-09-21
 * Time:0:58
 */
public class WebUtils {
    public static String getYMD() {
        LocalDateTime time = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd/");
        return time.format(dateTimeFormatter);
    }
    public static void saveFile(String dic, FileItem fileItem, HttpServletRequest req) throws Exception {
        String fileName = fileItem.getName();
        fileName = UUID.randomUUID() + "_" + fileName;
        String dicPath = "uploads/" + WebUtils.getYMD();
        String realDicPath = req.getServletContext().getRealPath(dicPath);
        File file = new File(realDicPath);
        if (!file.exists()) {
            boolean dicMake = file.mkdirs();
            if (!dicMake) throw new IOException();
        }
        String realFilePath = realDicPath + fileName;
        fileItem.write(new File(realFilePath));
    }
}
